package com.rtf.framework.web.http;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.rtf.framework.common.config.AppProperties;
import com.rtf.framework.common.secure.auth.AppUserDetails;
import com.rtf.framework.common.secure.auth.AppUserDetailsDefault;
import com.rtf.framework.common.secure.auth.AppUserDetailsService;
import com.rtf.framework.common.util.UtilString;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Slf4j
public class AppLoginBaseController extends AppIBaseController {

	@Autowired
	protected AppUserDetailsService appUserDetailsService ;

	@Autowired
	protected AppProperties appProperties ;

	/**
	 * 返回的用户信息类型
	 */
	@Setter
	@Getter
	private String allowedUserInfos = "role" ;

	@Setter
	@Getter
	private boolean returnToken = false ;

	@PostMapping("/api/user/info")
	@ResponseBody
	public ResponseEntity<ResponseParam> userInfo() {
		//1.0 查询处于登录状态的用户信息
		AppUserDetails appUserDetails = appUserDetailsService.getAppUserDetails();
		//1.1 创建用户信息参数
		Map<String,Object> resultParams = Maps.newHashMap() ;
		//1.2 返回当前系统配置信息
		resultParams.put("config" , appProperties.getGlobalParams() ) ;

		if( appProperties.getMockUser().getEnable() ){
			//2.1 mock状态，构建mock数据
			buildMockUserInfo( resultParams ) ;
			return ResponseParam.success()
					.data( resultParams )
					.getResponseEntity( HttpStatus.OK ) ;
		}else if( appUserDetails == null ){
			//2.2 未登录状态，构建未授权信息
			resultParams.put("reason" , HttpStatus.UNAUTHORIZED.getReasonPhrase() ) ;
			resultParams.put("code" , HttpStatus.UNAUTHORIZED.value() ) ;
			return ResponseParam.fail()
					.message("获取用户信息失败")
					.data( resultParams )
					.getResponseEntity( HttpStatus.OK ) ;
		}else {
			//2.3 登录状态，构建登录用户的信息
			//2.3.1 返回部分用户信息
			Map<String, Object> userInfo = Maps.newHashMap() ;
			userInfo.put("id", appUserDetails.getUserId()) ;
			userInfo.put("showName", UtilString.firstNotBlank( Lists.newArrayList(
					appUserDetails.getUserRealName() , appUserDetails.getTelephone() ) ) ) ;
			userInfo.put("name", appUserDetails.getUserRealName()) ;
			userInfo.put("telephone", appUserDetails.getTelephone()) ;

			addUserInfo( userInfo , appUserDetails ) ;
			resultParams.put("user", userInfo) ;

			//2.3.2  构建组织机构信息
			if(UtilString.contains( getAllowedUserInfos() , "org" )){
				Map<String, Object> org = Maps.newHashMap() ;
				org.put("orgId", appUserDetails.getOrgId()) ;
				org.put("orgCode", appUserDetails.getOrgCode()) ;
				org.put("orgName", appUserDetails.getOrgName()) ;

				resultParams.put("org", org) ;
			}

			//2.3.3  构建角色信息
			if(UtilString.contains( getAllowedUserInfos() , "role" )){
				if( appUserDetails instanceof AppUserDetailsDefault ){
					resultParams.put("roles" , ((AppUserDetailsDefault)appUserDetails).getUserAuths() ) ;
				}
			}

			//2.3.4  构建资源信息
			if(UtilString.contains( getAllowedUserInfos() , "resource" )){
				if( appUserDetails instanceof AppUserDetailsDefault ){
					resultParams.put("resources" , ((AppUserDetailsDefault)appUserDetails).getUserPermissions() ) ;
				}
			}

			//2.3.5  构建其他用户信息
			fillLoginParams( resultParams , appUserDetails ) ;

			return ResponseParam.success()
					.data( resultParams )
					.getResponseEntity( HttpStatus.OK ) ;
		}

	}

	/**
	 * 添加用户参数信息
	 * @param userParams
	 * @param appUserDetails
	 */
	public void addUserInfo(Map<String,Object> userParams , AppUserDetails appUserDetails){

	}

	/**
	 * 构建mock用户信息
	 * @param resultParams
	 */
	public void buildMockUserInfo( Map<String,Object> resultParams ){
		AppProperties.MockUser mockUser = appProperties.getMockUser() ;
		// 返回mock
		resultParams.put("id", mockUser.getUserId() ) ;
		resultParams.put("name", mockUser.getUserName()) ;
	}

	/**
	 * 填充登录信息参数
	 * @param resultParams
	 * @param appUserDetails
	 */
	public void fillLoginParams(Map<String,Object> resultParams , AppUserDetails appUserDetails){
		if( returnToken ){
			// 是否返回token
			try{
				resultParams.put("token" , getHttpServletRequest().getSession().getId()) ;
			}catch( Exception e ){
				log.error("填充用户信息失败 : {}" , e.getMessage() ) ;
			}
		}
	}

	@ResponseBody
	@PostMapping("/api/user/configure")
	public ResponseParam configure() {
		Map<String,Object> globalParams = appProperties.getGlobalParams() ;

		return ResponseParam.success().data( globalParams ) ;
	}

}
